package com.xmb.wechat.adapter;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatLastMsgBean;
import com.xmb.wechat.bean.WechatLastMsgBean_;
import com.xmb.wechat.bean.WechatMsgBean;
import com.xmb.wechat.bean.WechatMsgBean_;
import com.xmb.wechat.view.wechat.chat.WechatContactAddActivity;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.Box;


public class ContactAdapter extends BaseExpandableListAdapter {

    private final Activity                                mContext;
    private final ArrayList<ArrayList<WechatContactBean>> mDataList;
    private       OnContactItemClick                      onContactItemClick;
    private       NotifyUpdate                            updateLsn;

    public ContactAdapter(Activity context, NotifyUpdate updateLsn, ArrayList<ArrayList<WechatContactBean>> dataList, OnContactItemClick onContactItemClick) {
        mContext = context;
        this.updateLsn = updateLsn;
        this.onContactItemClick = onContactItemClick;
        mDataList = dataList;

    }

    @Override
    public int getGroupCount() {
        return mDataList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mDataList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    private GroupViewHold groupViewHold;

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                             ViewGroup parent) {
        ArrayList<WechatContactBean> group = mDataList.get(groupPosition);
        if (convertView == null) {
            groupViewHold = new GroupViewHold();
            convertView = View.inflate(mContext, R.layout.wechat_contact_item_tag, null);
            groupViewHold.tv = (TextView) convertView.findViewById(R.id.title);
            convertView.setTag(groupViewHold);
        } else {
            groupViewHold = (GroupViewHold) convertView.getTag();
        }

        groupViewHold.tv.setText(group.get(0).getFirstLetter());
        if (groupPosition == 0) {
            LinearLayout.LayoutParams layoutParams =
                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
            groupViewHold.tv.setLayoutParams(layoutParams);
        } else {
            LinearLayout.LayoutParams layoutParams =
                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
            groupViewHold.tv.setLayoutParams(layoutParams);
        }

        return convertView;
    }

    class GroupViewHold {
        TextView tv;
    }

    private ViewHolder viewHolder;

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ArrayList<WechatContactBean> list = mDataList.get(groupPosition);
        final WechatContactBean vo = list.get(childPosition);
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.wechat_contact_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_name = convertView.findViewById(R.id.name);
            viewHolder.iv_avatar = convertView.findViewById(R.id.iv_avatar);
            viewHolder.split_line = convertView.findViewById(R.id.split_line);
            viewHolder.vg = convertView.findViewById(R.id.vg);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        WechatContactBean.setupAvatarIntoImageView(vo, viewHolder.iv_avatar, 12);
        //        viewHolder.tv_name.setText(vo.getName() + "; " + vo.getFirstLetter() + ", " + vo
        //        .getPingyin());
        viewHolder.tv_name.setText(vo.getName());
        //组里最后一个Item的底部分隔线隐藏：
        if (list.size() == childPosition + 1) {
            viewHolder.split_line.setVisibility(View.GONE);
        } else {
            viewHolder.split_line.setVisibility(View.VISIBLE);
        }
        //长按事件处理：
        viewHolder.vg.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (vo.getInner()) {
                    return true;
                }
                showOnLongClick(vo);
                return true;
            }
        });
        //点击事件处理：
        viewHolder.vg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vo.getInner()) {
                    return;
                }
                if (onContactItemClick != null)
                    onContactItemClick.onItemClick(vo);
            }
        });

        return convertView;
    }

    //长按：
    private void showOnLongClick(final WechatContactBean vo) {
        new AlertDialog.Builder(mContext).setItems(new String[]{"编辑", "删除"},
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0) {
                            WechatContactAddActivity.start4Edit(mContext, vo);
                        } else if (which == 1) {
                            showOnDeleteContact(vo);
                        }
                    }
                }).setNegativeButton("取消", null).show();
    }

    private void showOnDeleteContact(final WechatContactBean vo) {
        new AlertDialog.Builder(mContext).setMessage("确定删除好友“" + vo.getName() +
                "”吗？与该好友的聊天记录将会一同删除")
                .setNegativeButton("取消", null).setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        WechatStartApplication.getBoxStore(mContext).boxFor(WechatContactBean.class).remove(vo);
                        ToastUtils.showLong("删除成功");
                        updateLsn.updateData();
                        //删除聊天消息
                        Box<WechatMsgBean> wechatMsgBeanBox = WechatStartApplication.getBoxStore(mContext).boxFor(WechatMsgBean.class);
                        List<WechatMsgBean> wechatMsgBeans = wechatMsgBeanBox.query().equal(WechatMsgBean_.talkerID, vo.getId()).build().find();
                        wechatMsgBeanBox.remove(wechatMsgBeans);
                        //删除首页最新消息
                        Box<WechatLastMsgBean> lastMsgBox = WechatStartApplication.getBoxStore(mContext).boxFor(WechatLastMsgBean.class);
                        WechatLastMsgBean lastMsg = lastMsgBox.query().equal(WechatLastMsgBean_.talkerID, vo.getId()).build().findFirst();
                        if (lastMsg != null)
                            lastMsgBox.remove(lastMsg);
                    }
                }).show();
    }

    class ViewHolder {
        public TextView  tv_name;
        public ImageView iv_avatar;
        public View      split_line;
        public View      vg;
    }

    public interface OnContactItemClick {
        void onItemClick(WechatContactBean wechatContactBean);
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public interface NotifyUpdate {
        void updateData();
    }

}
